package ace.module.captcha.sms.core.impl.manager.impl;

import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.module.captcha.sms.api.model.dto.CaptchaSmsApplicationExtendDto;
import ace.module.captcha.sms.core.impl.dao.entity.CaptchaSmsApplicationExtend;
import ace.module.captcha.sms.core.impl.manager.CaptchaSmsApplicationExtendManager;
import ace.module.captcha.sms.core.impl.manager.cache.CaptchaSmsApplicationExtendCacheDbManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* 短信验证码应用扩展 spring data jdbc querydsl rsql manager implement.
*
* @author Caspar
*/
@Slf4j
@AllArgsConstructor
@Component
public class CaptchaSmsApplicationExtendManagerImpl extends AbstractSpringDtoManager<CaptchaSmsApplicationExtend, Long, CaptchaSmsApplicationExtendDto>
    implements CaptchaSmsApplicationExtendManager {

  private final MessageSourceAssertService assertService;
  private final CaptchaSmsApplicationExtendCacheDbManager captchaSmsApplicationExtendCacheDbManager;

  @Override
  public CaptchaSmsApplicationExtend getAndCheckAllById(Long id) {
    CaptchaSmsApplicationExtend captchaSmsApplicationExtend =
        captchaSmsApplicationExtendCacheDbManager.getById(id);

    this.checkAll(captchaSmsApplicationExtend);

    return captchaSmsApplicationExtend;
  }

  @Override
  public CaptchaSmsApplicationExtend getAndCheckAllByCaptchaSmsAppId(Long captchaSmsAppId) {
    CaptchaSmsApplicationExtend captchaSmsApplicationExtend =
        captchaSmsApplicationExtendCacheDbManager.getByCaptchaSmsAppId(captchaSmsAppId);

    this.checkAll(captchaSmsApplicationExtend);

    return captchaSmsApplicationExtend;
  }

  public void checkAll(CaptchaSmsApplicationExtend captchaSmsApplicationExtend) {
    assertService.isNull(
        captchaSmsApplicationExtend,
        CaptchaSmsApplicationExtend.class.getName() + ".must-not-be-null-error",
        "请配置关联短信服务");

    assertService.isNull(
        captchaSmsApplicationExtend.getSmsAppId(),
        CaptchaSmsApplicationExtend.class.getName() + ".sms-app-id-must-not-be-null-error",
        "请配置关联短信服务");
  }
}
